package com.dnjn.happiness.emo.controller.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnjn.happiness.common.cloud.controller.BaseController;
import com.dnjn.happiness.common.core.utils.bean.BeanUtils;
import com.dnjn.happiness.common.core.web.domain.AjaxResult;
import com.dnjn.happiness.common.log.annotation.Log;
import com.dnjn.happiness.common.log.enums.BusinessType;
import com.dnjn.happiness.common.security.annotation.RequiresPermissions;
import com.dnjn.happiness.common.security.utils.SecurityUtils;
import com.dnjn.happiness.emo.domain.UserRecordFer;
import com.dnjn.happiness.emo.domain.vo.UserRecordFerAddVO;
import com.dnjn.happiness.emo.domain.vo.UserRecordFerVO;
import com.dnjn.happiness.emo.service.IUserRecordFerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Objects;

/**
 * 用户AI情绪userRecordFerController
 *
 * @author amber
 * @date 2024-04-09
 */

@Slf4j
public class CommonUserRecordFerController extends BaseController {
    @Autowired
    protected IUserRecordFerService userRecordFerService;


    /**
     * 获取用户AI情绪userRecordFer详细信息
     */
    @Log(title = "用户AI情绪userRecordFer", businessType = BusinessType.SELECT)
    @RequiresPermissions("@ss.hasAnyPermi('emo:userRecordFer:query,ignore:userRecordFer:query')")
    @GetMapping(value = "/{ferId}")
    public AjaxResult get(@PathVariable("ferId") Long ferId) {
        return success(userRecordFerService.getById(ferId));
    }

    /**
     * 新增用户AI情绪userRecordFer
     */
    @Log(title = "用户AI情绪userRecordFer", businessType = BusinessType.INSERT)
    @RequiresPermissions("@ss.hasAnyPermi('emo:userRecordFer:add,ignore:userRecordFer:add')")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody UserRecordFerAddVO vo) {
        StopWatch stopWatch = new StopWatch("fer_add");
        stopWatch.start();
        validLinkedUrl(vo.getImageUrl());
        Long userId = SecurityUtils.getUserId();
        UserRecordFer record = new UserRecordFer();
        BeanUtils.copyPropsIgnoreNull(vo, record);
        record.setUserId(userId);
        record.setNickName(SecurityUtils.getLoginUser().getNickName());
        record.setCreateBy(SecurityUtils.getUsername());
        record.setCurrDate(LocalDate.now());
        boolean isOk = userRecordFerService.createOrUpdate(record);
        stopWatch.stop();

        log.info("stopwatch {}ms {}", stopWatch.getTotalTimeMillis(), stopWatch.prettyPrint());
        return toAjax(isOk);
    }

    /**
     * 修改用户AI情绪userRecordFer
     */
    @Log(title = "用户AI情绪userRecordFer", businessType = BusinessType.UPDATE)
    @RequiresPermissions("@ss.hasAnyPermi('emo:userRecordFer:edit,ignore:userRecordFer:edit')")
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody UserRecordFerVO vo) {
        validLinkedUrl(vo.getImageUrl());

        UserRecordFer userRecordFer = new UserRecordFer();
        BeanUtils.copyPropsIgnoreNull(vo, userRecordFer);
        return toAjax(userRecordFerService.updateById(userRecordFer));
    }

    /**
     * 删除用户AI情绪userRecordFer
     */
    @Log(title = "用户AI情绪userRecordFer", businessType = BusinessType.DELETE)
    @RequiresPermissions("@ss.hasAnyPermi('emo:userRecordFer:remove,ignore:userRecordFer:remove')")
    @DeleteMapping("/{ferIds}")
    public AjaxResult remove(@PathVariable Long[] ferIds) {
        return toAjax(userRecordFerService.removeBatchByIds(Arrays.asList(ferIds)));
    }

    /**
     * 统计当日新增人脸数量
     */
    @GetMapping("/todayUserRecordFerTotal")
    @RequiresPermissions("@ss.hasAnyPermi('emo:userRecordFer:query,ignore:userRecordFer:query')")
    public AjaxResult todayUserRecordFerTotal(@RequestParam(required = false) Long deptId) {
        UserRecordFer userRecordFer = new UserRecordFer();
        userRecordFer.setCurrDate(LocalDate.now());
        Page page = Page.of(1, 10);

        if (Objects.isNull(deptId)) {
            deptId = SecurityUtils.getLoginUser().getDeptId();
        }
        userRecordFer.setDeptId(deptId);

        Page<UserRecordFer> list = userRecordFerService.getPage(userRecordFer, page);
        return AjaxResult.success(list.getTotal());
    }
}
